package com.group.itms.routes.map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.group.itms.routes.R;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.ObjectType;

public class Marker implements IMarker {

	private PointD mLocation;
	private Bitmap mIcon;
	private BObject mObject;
	
	private Layer mLayer;
	
	private int mClickCount;
	private View mInfoView;
	private String mInfoText;
	
	public static Marker getInstance(Resources res, BObject object) {
		Marker marker = new Marker();
		ObjectType objType = object.getType();
		if (objType.equals(ObjectType.INF_DESC)) {
			 //marker.setImageResource(R.drawable.ic_);
		} else if (objType.equals(ObjectType.INF_POINT)) {
			marker.setIcon(BitmapFactory.decodeResource(res, R.drawable.ic_point));
		} else if (objType.equals(ObjectType.TASK)) {
			marker.setIcon(BitmapFactory.decodeResource(res, R.drawable.ic_task));
		}
		return marker;
	}

	public PointD getLocation() {
		return mLocation;
	}
	
	public void setLocation(PointD location) {
		this.mLocation = location;
	}

	public PointD getScreenLocation(Projection proj) {
		return proj.convertToScreen(mLocation);
	}
	
	public double screenDistance(Projection proj, float scX, float scY) {
		PointD scL = getScreenLocation(proj);
		return Math.hypot(scL.x - scX, scL.y - scY);
	}

	public void setIcon(Bitmap icon) {
		mIcon = icon;
	}

	public Projection getProjection() {
		if (mLayer != null) {
			return mLayer.getProjection();
		}
		return null;
	}
	
	public Bitmap getIcon() {
		return mIcon;
	}

	public BObject getObject() {
		return mObject;
	}

	public void setObject(BObject object) {
		mObject = object;
	}

	public void setLayer(Layer layer) {
		mLayer = layer;
	}
	
	public Layer getLayer() {
		return mLayer;
	}
	
	public double getX() {
		if (mLocation != null) {
			return mLocation.x;
		}
		return 0;
	}
	
	public double getY() {
		if (mLocation != null) {
			return mLocation.y;
		}
		return 0;
	}
	
	@Override
	public void setOnClickListener(OnClickListener onClick) {
		if (mInfoView != null) {
			mInfoView.setOnClickListener(onClick);
		}
	}
	
	void performClick() {
		Projection p = getProjection();
		PointD l = p.rawConvertToScreen(mLocation);
		mLayer.getMapView().smoothZoomTo(p.getZoom(), (float)l.x, (float)l.y);
		if (++mClickCount == 2) {
			mClickCount = 0;
		}
		mLayer.redrawMap();
	}
	
	int getClickCount() {
		return mClickCount;
	}
	
	public void setInfoView(View v) {
		mInfoView = v;
		if (mInfoText != null) {
			TextView textView = (TextView)mInfoView.findViewById(R.id.tvInfoText);
			textView.setText(mInfoText);
		}
	}

	public View getInfoView() {
		return mInfoView;
	}
	
	public void setInfoText(String text) {
		mInfoText = text;
		if (mInfoView != null) {
			TextView textView = (TextView)mInfoView.findViewById(R.id.tvInfoText);
			textView.setText(mInfoText);
		}
	}
	
	public String getInfoText() {
		return mInfoText;
	}
	
	@Override
	public String toString() {
		return "Marker[location=" + mLocation + "]";
	}

}